Skip to content

Skip Cloudflare runtime startup during type generation - #16961

Merged
matthewp merged 2 commits into
withastro:mainfrom
adamchal:fix/cloudflare-sync-skip-runtime
Jun 19, 2026
Merged

matthewp merged 2 commits into
withastro:mainfrom
adamchal:fix/cloudflare-sync-skip-runtime

Conversation

@adamchal

@adamchal adamchal commented Jun 3, 2026

Copy link
Copy Markdown
Member

Changes

astro sync was much slower with @astrojs/cloudflare because type generation started the Cloudflare dev runtime even though no requests are served. During the invocation of the temporary type generation dev server, the adapter now:

  • Clears configureServer from @cloudflare/vite-plugin plugins so the Cloudflare dev runtime never starts.
  • Sets optimizeDeps: { noDiscovery: true, include: [] } on every environment so no dependencies are pre-bundled.
  • dev and preview continue to start the Cloudflare runtime as expected.

Testing

  • Measured astro sync type generation with the MRE:
Adapter astro sync
none 37 ms
@astrojs/node 37 ms
@astrojs/vercel 36 ms
@astrojs/netlify 29 ms
@astrojs/cloudflare (before) 1.16 s
@astrojs/cloudflare (after) 56 ms

Docs

  • No docs needed. This is a build-time performance fix with no user-facing API change.

Closes #16332

@changeset-bot

changeset-bot Bot commented Jun 3, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 95b9339

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 37 packages
Name Type
@astrojs/cloudflare Patch
@test/astro-cloudflare-node-prerender-mdx Patch
@test/astro-cloudflare Patch
@test/astro-cloudflare-allowed-hosts Patch
@test/astro-cloudflare-astro-dev-platform Patch
@test/astro-cloudflare-astro-env Patch
@test/astro-cloudflare-binding-image-service Patch
@test/astro-cloudflare-cache-provider-wait-until Patch
@test/astro-cloudflare-client-address Patch
@test/astro-cloudflare-compile-image-service Patch
@test/astro-cloudflare-custom-entryfile Patch
@test/astro-cloudflare-dev-image-endpoint Patch
@test/astro-cloudflare-external-image-service Patch
@test/astro-cloudflare-external-redirects Patch
@test/astro-cloudflare-internal-redirects Patch
@test/astro-cloudflare-no-output Patch
@test/astro-cloudflare-prerender-node-env Patch
@test/astro-cloudflare-prerender-queue-consumers Patch
@test/astro-cloudflare-prerender-styles Patch
@test/astro-cloudflare-prerenderer-errors Patch
@test/routing-priority-cloudflare Patch
@test/cf-server-entry Patch
@test/astro-cloudflare-server-island-prerender-framework Patch
@test/astro-cloudflare-sessions Patch
@test/astro-cloudflare-sql-import Patch
@test/cf-ssr-deps Patch
@test/astro-cloudflare-static Patch
@test/astro-cloudflare-svelte-rune-deps Patch
@test/astro-cloudflare-top-level-return Patch
@test/cf-user-optimize-deps Patch
@test/astro-cloudflare-vite-plugin Patch
@test/astro-cloudflare-with-base Patch
@test/astro-cloudflare-with-react Patch
@test/astro-cloudflare-with-solid-js Patch
@test/astro-cloudflare-with-svelte Patch
@test/astro-cloudflare-with-vue Patch
@test/astro-cloudflare-wrangler-preview-platform Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

// Avoid starting the Cloudflare dev runtime during type generation. See #16332.
if (isTypeGenOnly) {
for (const plugin of cloudflareVitePlugins) {
plugin.configureServer = undefined;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this currently start the dev server during the build? I assume it probably does not.. but not obvious from the logic.

I'm betting that the server starts in sync by not in the build. in which case I would suggest getting rid of the variable and just inlining the conditions with a comment, as the variable could make someone thing something is wrong that the configureServer function is removed from the build.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

build does start the Cloudflare runtime during its internal type-gen pass: it calls syncInternal({ command: 'build' }) (core/build/index.ts#L174), which spins up a temporary Vite server — and creating that server fires every plugin’s configureServer, including @cloudflare/vite-plugin’s. The only way to skip it is the programmatic sync: false path, which isn’t what astro build does.

So the cleanest fix is to strip configureServer from the plugin during this phase. The other time saver is skipping dep pre-bundling in the same phase.

We could inline the condition in both places or maybe rename the variable to something like isTypeGenPhase or skipCloudflareRuntime. What do you think?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Variable name doesn't matter so much as understanding what you just said, so if you could just add a comment that explains why its needed for both that will be enough.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clarity added in 95b9339

@matthewp
matthewp merged commit 96398e8 into withastro:main Jun 19, 2026
24 checks passed
matthewp pushed a commit that referenced this pull request Jun 19, 2026
* fix(cloudflare): skip runtime startup during type generation

* docs(cloudflare): explain why type-gen runtime skip covers build and sync
@astrobot-houston astrobot-houston mentioned this pull request Jun 19, 2026
@adamchal
adamchal deleted the fix/cloudflare-sync-skip-runtime branch July 1, 2026 05:36
alexanderniebuhr added a commit to swaggysnippets/astro that referenced this pull request Aug 7, 2026
The actions-build fixture test could not fail when the withastro#16961 guard was
reverted: it pinned prerenderEnvironment 'node' (skipping the default
workerd prerender path), its second build was a pure optimizer cache hit
rather than a stale-cache scenario, and its assertions never touched
Actions output. Replace it with a unit test that invokes the adapter's
astro:config:setup hook directly and asserts both observable effects of
the isTypeGenPhase guard: configureServer is stripped from the
Cloudflare Vite plugins and dependency discovery is disabled for every
environment during build/sync, while dev keeps both. Verified the new
test fails when the guard is neutered to sync-only or removed.

Restore buildWithRetry: the stale-prebundle race it guards (withastro#14821) was
never root-caused or fixed, and the suite runs all 49 files serially in
one process with no CI-level retry to absorb a flake.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
alexanderniebuhr added a commit to swaggysnippets/astro that referenced this pull request Aug 7, 2026
The actions-build fixture test could not fail when the withastro#16961 guard was
reverted: it pinned prerenderEnvironment 'node' (skipping the default
workerd prerender path), its second build was a pure optimizer cache hit
rather than a stale-cache scenario, and its assertions never touched
Actions output. Replace it with a unit test that invokes the adapter's
astro:config:setup hook directly and asserts both observable effects of
the isTypeGenPhase guard: configureServer is stripped from the
Cloudflare Vite plugins and dependency discovery is disabled for every
environment during build/sync, while dev keeps both. Verified the new
test fails when the guard is neutered to sync-only or removed.

Restore buildWithRetry: the stale-prebundle race it guards (withastro#14821) was
never root-caused or fixed, and the suite runs all 49 files serially in
one process with no CI-level retry to absorb a flake.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pkg: integration Related to any renderer integration (scope)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Performance]: Slow astro sync with the Cloudflare adapter

2 participants